#!/usr/bin/python3

import cv2
import numpy as np


def _robert_(img):
    __img__ = img.copy()
    r, c = img.shape
    ro = [[-1, 0], [0, 1]]
    for x in range(r):
        for y in range(c):
            if (y + 2 <= c) and (x + 2 <= r):
                img_child = img[x:x+2, y:y+2]
                #print(img_child)
                list_robert = ro*img_child
                #print(list_robert)
                __img__[x, y] = abs(list_robert.sum())      # 求和加绝对值
    return __img__


img = cv2.imread('/Users/ben/Documents/AI/CSDN/pics/lena.jpg', cv2.IMREAD_GRAYSCALE)

cv2.imshow('GRAY Source:', img)
cv2.imwrite('/Users/ben/Documents/AI/CSDN/pics/lena_grayscale.jpg', img)

# # robers算子
out_robert = _robert_(img)
cv2.imshow('robert:', out_robert)

cv2.imwrite('/Users/ben/Documents/AI/CSDN/pics/lena_robert.jpg', out_robert)


'''
img_test = np.zeros(shape=[5, 5])
img_test[:, 3] = 255
img_test[:, 4] = 255
print(img_test)

cv2.imshow("test", img_test)

cv2.edgePreservingFilter()
G = edge(I, 'roberts');

out_robert = _robert_(img_test)
cv2.imshow("robert - ", out_robert)
print(out_robert)
'''
cv2.waitKey()
cv2.destroyAllWindows()
